package com.prs.crm.action.fs;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.OutlayItem;
import com.prs.crm.service.fs.OutlayItemService;
import com.prs.crm.service.sys.LogService;

public class OutlayItemAction extends BaseAction {
	private OutlayItem outlayItem;
	private OutlayItemService outlayItemService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listOutlayItem")
	public String listOutlayItem() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				OutlayItem.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getOutlayItemService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editOutlayItem")
	public String editOutlayItem() {
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveOutlayItem", results = {
			@Result(name = "success", type = "redirect", location = "listOutlayItem"),
			@Result(name = "error", type = "chain", location = "editOutlayItem") })
	public String saveOutlayItem() {
		if (outlayItem.getName() == null
				|| "".equals(outlayItem.getName().trim())) {
			addActionError("费用项目不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(OutlayItem.class,
				outlayItem.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getOutlayItemService().save(outlayItem);
		this.setPrompt("提交费用项目成功");
		return SUCCESS;
	}

	@Action(value = "removeOutlayItem", results = {
			@Result(name = "success", type = "redirect", location = "listOutlayItem"),
			@Result(name = "error", type = "chain", location = "listOutlayItem") })
	public String removeOutlayItem() {
		if (ids == null) {
			this.addActionError("请选择要删除的费用项目");
			return ERROR;
		}
		for (Integer id : ids) {
			OutlayItem outlayItem = this.getOutlayItemService().get(id);
			if (outlayItem.getOutlayDetails().size() != 0) {
				this.addActionError("已有客户引用，不能删除");
				return ERROR;
			}
		}
		this.getOutlayItemService().batchRemove(ids);
		return SUCCESS;
	}

	public OutlayItem getOutlayItem() {
		return outlayItem;
	}

	public void setOutlayItem(OutlayItem outlayItem) {
		this.outlayItem = outlayItem;
	}

	public OutlayItemService getOutlayItemService() {
		return outlayItemService;
	}

	public void setOutlayItemService(OutlayItemService outlayItemService) {
		this.outlayItemService = outlayItemService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

}
